<?php


namespace seecms\lib\builder\control;

class SelectControl extends Control
{
    protected $type = 'select';

    protected $multiple = false;

    /**
     * @return bool
     */
    public function isMultiple(): bool
    {
        return $this->multiple;
    }

    /**
     * @param bool $multiple
     * @return SelectControl
     */
    public function setMultiple(bool $multiple): SelectControl
    {
        $this->multiple = $multiple;

        return $this;
    }

    public function render(): string
    {

        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';
        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();
        $options = $this->getOptions();

        $option = '';
        foreach ($options as $key => $item) {

            $selected = '';
            if (!is_null($this->value) && $this->value == $key) {
                $selected = 'selected';
            }

            $option .= <<<EOF
<option value="{$key}" {$selected}>{$item}</option>
EOF;
        }

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }

        return <<<EOF
$container
    <label class="layui-form-label">
        {$this->label}
        {$required}
    </label>
    <div class="layui-input-$display">
        <select name="{$this->name}"
                lay-search
                $readonly
                $disabled
        >
            <option value="">{$this->placeholder}</option>
            $option
        </select>
        $description
    </div>
</div>
EOF;

    }
}